package ru.dc.gui.ncurses;

import charva.awt.BorderLayout;
import charva.awt.FlowLayout;
import charva.awt.Toolkit;
import charva.awt.event.ActionEvent;
import charva.awt.event.ActionListener;
import charva.awt.event.KeyAdapter;
import charva.awt.event.KeyEvent;
import charvax.swing.*;
import ru.dc.gui.UsersStore;
import ru.dc.object.ServerInfo;


/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 11.10.2010
 * Time: 13:05:20
 * </pre>
 */
public class HubWindow extends JPanel {

    private ServerInfo serverInfo;
    private JTextField message;
    private JTextArea messages;
    private JTextField topic;
    private JList usersList;
    private HubWindowConnectionListener connectionListener;

    public HubWindow() {

        connectionListener = new HubWindowConnectionListener(this);

        setLayout(new BorderLayout());

        setWidth(120);
        topic = new JTextField();
        topic.setColumns(100);
        add(topic, BorderLayout.NORTH);

        messages = new JTextArea();
        messages.setLineWrap(true);
        messages.setWrapStyleWord(true);
        messages.setEditable(false);
        messages.setColumns(60);
        messages.setRows(40);
        final JScrollPane scrollPane = new JScrollPane(messages);
        scrollPane.setWidth(Toolkit.getDefaultToolkit().getScreenColumns()-2);
        scrollPane.setHeight(Toolkit.getDefaultToolkit().getScreenRows()-10);
        add(scrollPane);

        JPanel messagePanel = new JPanel(new BorderLayout());
        message = new JTextField();
        message.setColumns(40);
        messagePanel.add(new JLabel("Text: "), BorderLayout.WEST);
        messagePanel.add(message, BorderLayout.CENTER);

        JPanel btnPanel = new JPanel(new FlowLayout());
        messagePanel.add(btnPanel, BorderLayout.EAST);

        final JButton sendMessageBtn = new JButton("Send");
        final JButton searchBtn = new JButton("Search");
        btnPanel.add(sendMessageBtn);
        btnPanel.add(searchBtn);

        sendMessageBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                sendMessage();
            }
        });

        searchBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                search();
            }
        });

        message.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent ke) {
                if (ke.getKeyCode()==KeyEvent.VK_ENTER) {
                    sendMessage();
                }
            }
        });

        add(messagePanel, BorderLayout.SOUTH);

        usersList = new JList();
        final UsersListModel listModel = new UsersListModel(new UsersStore());
        usersList.setModel(listModel);
        usersList.setColumns(20);
        usersList.setVisibleRowCount(40);
        final JScrollPane sp = new JScrollPane(usersList);
        sp.setWidth(8);
        add(sp, BorderLayout.EAST);



    }


    private void search() {
/*
        final Search search = new Search();
        search.setActive(false);
        search.setRequestorNick(serverInfo.getServerInfo().getNick());
        search.setSearchPattern(message.getText());
        new Thread() {
            @Override
            public void run() {
                serverInfo.sendCommand(search);
            }
        }.start();

*/
    }

    private void sendMessage() {
/*
        if (StringUtils.isEmpty(message.getText())) {
            return;
        }
        serverInfo.chat(message.getText());
*/
    }

    public ServerInfo getServerInfo() {
        return serverInfo;
    }

    public void setServerInfo(ServerInfo serverInfo) {
        ServerInfo old = this.getServerInfo();
        this.serverInfo = serverInfo;
    }

    public void close() {
        //force remove listener
        setServerInfo(null);
    }


    public JTextField getMessage() {
        return message;
    }

    public JTextArea getMessages() {
        return messages;
    }

    public JTextField getTopic() {
        return topic;
    }

    public JList getUsersList() {
        return usersList;
    }
}
